package com.oracleoaec.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.oracle.template.JDBCTemplate;
import com.oracle.template.PreparedStatementSetter;
import com.oracle.template.ResultSetHandler;
import com.oracleoaec.dao.CarDao;
import com.oracleoaec.dao.RecordDao;
import com.oracleoaec.dao.UserDao;
import com.oracleoaec.entity.Car;
import com.oracleoaec.entity.Record;
import com.oracleoaec.entity.User;
import com.oracleoaec.util.DBUtils;

public class RecordDaoImpl extends JDBCTemplate implements RecordDao {
	
	private Record record=null;
	UserDao userDao=new UserDaoImpl();
	CarDao carDao=new CarDaoImpl();
	String[] s1=null;
	@Override
	public Record userRentCar(String carIdAndUserId) {
		// String str=1+2&2
		// 保证status为可租(0), userable为上架(0)
		
		//rentCar?1+4&2
		//t_car的ID==4==1 用户的ID=2==2
		//System.out.println("carIdAndUserId=="+carIdAndUserId);
		String[] ss=carIdAndUserId.split("\\?");
		s1 = ss[1].split("\\+");
		final String[] s2 = s1[1].split("&");
		//System.out.println("t_car的ID==" + s2[0] + "= 用户的ID=" + s2[1]+"=");
		String sql=null;
		String sql2=null;
		String sql3=null;
		//System.out.println("s1[0]="+s1[0]);
		
		if("1".equals(s1[0])){//输入1表示借车
			//System.out.println("借车");
			sql	="update t_car set status=1 where status=0 and id=? and userable=0";
			sql2="insert into t_record values(gwm5.nextval,sysdate,null,null,?,?)";// 第一个是userId,第二个是carId
			sql3="select * from t_record where user_id=? and car_id=? and return_date is null";
		}else if("7".equals(s1[0])){//输入7就表示还车
			//System.out.println("还车");
			sql = "update t_car set status=0 where status=1 and id=? and userable=0";
			//更新时间的sql语句
			sql2="update t_record set payment=(select (trunc((sysdate-(select start_date from t_record where user_id=? and car_id=? and payment is null)),0)+1)*(select nvl(rent,'0.0') from t_car where id=?) from dual),return_date=sysdate where user_id=? and car_id=?";
			//System.out.println("sql222 这里是还车");
			sql3= "select * from t_record where user_id=? and car_id=? and return_date is not null";
		}
		
		Connection conn = DBUtils.getConn();// 获取Connection连接
		try {
			// 设置自动的为false
			conn.setAutoCommit(false);
			//更新
			if(update(conn, sql, new PreparedStatementSetter() {
				@Override
				public void setValues(PreparedStatement ps) throws SQLException {
					ps.setLong(1, Integer.parseInt(s2[0]));
				}
			})>0){
				//插入
				if(update(conn, sql2, new PreparedStatementSetter() {
					@Override
					public void setValues(PreparedStatement ps) throws SQLException {
						
						ps.setLong(1, Integer.parseInt(s2[1]));//用户
						ps.setLong(2, Integer.parseInt(s2[0]));//car的ID
						if("7".equals(s1[0])){
							ps.setLong(3, Integer.parseInt(s2[0]));//carId
							ps.setLong(4,Integer.parseInt(s2[1]));//用户ID
							ps.setLong(5, Integer.parseInt(s2[0]));//car的ID
						}
					}
					
					// 设置自动提交
				})>0){
					conn.commit();
					//开始查询记录返回给客户端
					query(sql3, new PreparedStatementSetter() {
						@Override
						public void setValues(PreparedStatement ps) throws SQLException {
							ps.setLong(1, Integer.parseInt(s2[1]));
							ps.setLong(2, Integer.parseInt(s2[0]));
						}
					}, new ResultSetHandler() {
						@Override
						public void handleRs(ResultSet rs) throws SQLException {
							if(rs.next()) {
								//System.out.println("rs==" + rs.getString("start_date"));
								//将租车记录返回给用户观看
								long id=rs.getLong("id");//这个是记录ID
								String startDate=rs.getString("start_date");
								String returnDate=rs.getString("return_date");
								System.out.println("returnDate3="+returnDate);
								double payment=rs.getDouble("payment");//0.35
								System.out.println("payment3="+payment);
								Car car=carDao.queryCarById(rs.getLong("car_id"));
								User user=userDao.queryById(rs.getLong("user_id"));
								record=new Record(id, startDate,returnDate,payment, user, car);
							}
						}
					});
				}
			}
		} catch (SQLException e) {
			try {
				// 设置回滚的对象
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			try {
				if(conn!=null){
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return record;
	}
	@Override
	public Record userBackCar(String carIdAndUserId) {
		
		return null;
	}
	
	List<Record> list=null;
	//查看所有的租车记
	@Override
	public List<Record> queryAllUserRecord(final String userId) {
		list=new ArrayList<Record>();
		String sql= "select * from t_record where user_id=?";
		//System.out.println("sql=="+sql);
		//System.out.println("userId="+userId);
		query(sql,new PreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setLong(1, Integer.parseInt(userId));
			}
		}, new ResultSetHandler() {
			
			@Override
			public void handleRs(ResultSet rs) throws SQLException {
				while(rs.next()) {
					//System.out.println("rs==" + rs.getString("start_date"));
					//将租车记录返回给用户观看
					long id=rs.getLong("id");//这个是记录ID
					String startDate=rs.getString("start_date");
					String returnDate=rs.getString("return_date");
					double payment=rs.getDouble("payment");
					Car car=carDao.queryCarById(rs.getLong("car_id"));
					User user=userDao.queryById(rs.getLong("user_id"));
					record=new Record(id, startDate,returnDate,payment, user, car);
					//System.out.println("query222"+record);
					list.add(record);
				}
			}
		});
		return list;
	}
}
